package com.AEWMS.Service;

import com.AEWMS.Dao.*;
import com.AEWMS.Dao.Class;
import com.AEWMS.Utlis.DbCon;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author lhs
 * @create 2021/2/22 16:16
 */
public class MajorService {

    private Connection conn = null ;
    private PreparedStatement pstmt = null ;

    public MajorService() {
        DbCon dbcon;
        try {
            dbcon = new DbCon();
            this.conn = dbcon.getConnection();
        } catch (Exception e) {
            System.out.println("MajorService 连接数据库失败");
            e.printStackTrace();
        }
    }

    public void closeCon(){
        try {
            this.conn.close();
        } catch (Exception e) {
            System.out.println("数据库连接关闭");
            e.printStackTrace();
        }
    }

    /**
     * 查找所有专业
     * @return 所有专业信息
     */
    public ArrayList<Major> findAllMajor(){
        ArrayList<Major> allCollegeList = new ArrayList();//升级版数组，返回所有专业对象
        String sql = "SELECT * FROM major_info";
        try {
            this.pstmt = conn.prepareStatement(sql);
            ResultSet rs = this.pstmt.executeQuery();
            while (rs.next()){
                Major s = new Major();//按行查找，为false时查询结束
                s.setMajorId(rs.getInt(1));
                s.setMajorName(rs.getString(2));
                s.setCollegeId(rs.getString(3));
                allCollegeList.add(s);//将查找到的行（每个专业对象）添加到数组
            }
        } catch (Exception e) {
            System.out.println("findAllMajor 失败");
            System.out.println(e);
        }
        return allCollegeList;//输出包含所有专业对象的数组
    }

    /**
     * 查找专业
     * @param id 通过id
     * @return 返回一个专业对象
     */
    public Major findMajorById(String id){
        Major s = new Major();
        String sql = "SELECT * FROM major_info WHERE major_id = ?";
        try {
            this.pstmt = conn.prepareStatement(sql);//将sql语句放到数据库的准备对象
            this.pstmt.setString(1,id);//将传入id值替换问号放在SQL
            ResultSet rs = this.pstmt.executeQuery();//执行查询返回结果集
            while (rs.next()){
                s.setMajorId(rs.getInt(1));
                s.setMajorName(rs.getString(2));
                s.setCollegeId(rs.getString(3));
            }
        }
        catch (Exception e) {
            System.out.println("findMajorById 失败");
            System.out.println(e);
        }
        return s;
    }

    /**
     * 添加学期专业
     * @return 一个专业对象
     */
    public boolean insertMajor(Major s){
        boolean flag = false;
        String sql = "INSERT INTO major_info VALUES(?,?,?)";
        try {
            this.pstmt = conn.prepareStatement(sql);//sql语句已经在这个准备对象里，有问号就要用准备对象
            this.pstmt.setInt(1,s.getMajorId());//自增也要传入
            this.pstmt.setString(2,s.getMajorName());
            this.pstmt.setString(3,s.getCollegeId());

            int rs = this.pstmt.executeUpdate();//这里不用再传sql
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("insertMajor defeat");
            System.out.println(e);
        }
        return flag;
    }

    /**
     * 修改一个专业
     * @param s 一个专业对象
     * @return 是否成功
     */
    public boolean updateMajor(Major s) {
        boolean flag = false;
        String sql = "UPDATE major_info SET major_name = ?, college_id = ? WHERE  major_id = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,s.getMajorName());
            this.pstmt.setString(2,s.getCollegeId());
            this.pstmt.setInt(3,s.getMajorId());
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        } catch (Exception e) {
            System.out.println("updateMajor 失败");
            System.out.println(e);
        }
        return flag;

    }

    /**
     * 删除一个专业
     * @param id 专业id
     * @return 是否成功
     */
    public boolean deleteMajorById(String id){
        boolean flag = false;
        String sql = "DELETE FROM major_info WHERE major_id = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,id);//这里的名字与dao.admin传进来的一致
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("deleteMajorById 失败");
            System.out.println(e);
        }
        return flag;
    }

}



